package com.demo.view;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

public abstract class PopupWindowHelper {
    public static final float POPUP_WINDOW_ALPHA_ALL = 1f;
    public static final float SHOW_POPUP_WINDOW_ALPHA = 0.7f;
    public PopupWindowHelper(){
        mAlpha = SHOW_POPUP_WINDOW_ALPHA;
    }

    public PopupWindowHelper(float alpha){
        mAlpha = alpha;
    }

    private PopupWindow mPopupWindow;
    private final float mAlpha;
    public PopupWindow getPopupWindow() {
        return mPopupWindow;
    }

    public boolean isShowing(){
        return mPopupWindow != null && mPopupWindow.isShowing();
    }

    public void showPopupWindow(Activity activity, @LayoutRes int resource, View parent, int gravity) {
        showPopupWindow(activity, resource, parent, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, gravity);
    }

    public void showPopupWindow(Activity activity, @LayoutRes int resource, View parent, int width, int height, int gravity) {
        View view = LayoutInflater.from(activity).inflate(resource, null);
        initView(view);
        createPopupWindow(activity, view, width, height);
        setPopupWindowListener(view);
        View parentView = parent;
        if (parent == null){
            parentView = view;
        }
        setTouchable();
        showAtLocation(parentView, gravity, 0, 0);
    }


    public void createPopupWindow(Activity activity, View contentView, int width, int height) {
        mPopupWindow = new PopupWindow(contentView, width, height, true);
        if (mAlpha != POPUP_WINDOW_ALPHA_ALL){
            setAttributes(activity, mAlpha);
        }
    }

    private void setAttributes(Activity activity, float showPopupWindowAlpha) {
        if (activity != null && activity.getWindow() != null){
            WindowManager.LayoutParams params = activity.getWindow().getAttributes();
            params.alpha = showPopupWindowAlpha;
            activity.getWindow().setAttributes(params);
        }
    }

    public void setOldAttributes(Activity activity){
        setAttributes(activity, PopupWindowHelper.POPUP_WINDOW_ALPHA_ALL);
    }

    public void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        if (mPopupWindow != null){
            mPopupWindow.setOnDismissListener(onDismissListener);
        }
    }

    protected void initView(@NonNull View view){}
    protected abstract void setPopupWindowListener(@NonNull View view);

    public void showAtLocation(View parent, int gravity, int x, int y) {
        if (parent.getContext() instanceof Activity) {
            Activity activity = (Activity) parent.getContext();
            if (activity.isFinishing() || activity.isDestroyed()) {
                return;
            }
        } else {
            // 非Activity不能用于弹窗
            return;
        }
        try {
            mPopupWindow.showAtLocation(parent, gravity, x, y);
        } catch (Throwable ex) {

        }
    }

    protected void setTouchable() {
        setTouchableImpl(true, true, true);
    }

    public void setTouchableImpl(boolean setTouchable, boolean setOutsideTouchable, boolean setFocusable) {
        mPopupWindow.setTouchable(setTouchable);
        mPopupWindow.setOutsideTouchable(setOutsideTouchable);
        mPopupWindow.setFocusable(setFocusable);
    }
    public void dismissPopupWindow() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

}

